#pragma once

#include <iostream>
#include <string>
#include <stdarg.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

using namespace std;

enum
{
    Debug = 0,
    Info,
    Warning,
    Error,
    Fatal
};

enum
{
    Screen = 10,
    OneFile,
    ClassFile
};

string LevelToString(int level)
{
    switch (level)
    {
    case Debug:
        return "Debug";
    case Info:
        return "Info";
    case Warning:
        return "Warning";
    case Error:
        return "Error";
    case Fatal:
        return "Fatal";
    default:
        return "Unknown"; 
    }
}
 
const int defaultStyle = Screen; 
const string defaultFileName = "log.";
const string logdir = "log";

class Log
{
public:
    Log():_style(defaultStyle), _fileName(defaultFileName)
    {
        // mkdir(logdir.c_str(), 0775);
    }
    // 支持把日志信息写入到其他位置的用户接口
    // OneFile、ClassFile
    void Enable(int style)
    {
        _style = style;
    }
    string StampExLocalTime()
    {
        time_t currtime = time(nullptr);
        struct tm *time = localtime(&currtime);
        char timeBuffer[128];
        snprintf(timeBuffer, sizeof(timeBuffer), "%d-%d-%d %d:%02d:%02d", 
        time->tm_year + 1900, time->tm_mon+1, time->tm_mday, time->tm_hour, time->tm_min, time->tm_sec);
        return timeBuffer;
    }

    void WriteToOneFile(const string &name, const string &message)
    {
        umask(0);
        int fd = open(name.c_str(), O_CREAT | O_WRONLY | O_APPEND, 0666);
        if(fd < 0) return;
        write(fd, message.c_str(), message.size());
        close(fd);
    }

    // 文件名类似于： log.Debug、log.Warning...
    void WriteToClassFile(const string &levelstr, const string &message)
    {
        string name = logdir;
        name += "/";
        name += _fileName;
        name += levelstr; 
        WriteToOneFile(name, message);
    }
    void WriteLog(const string &levelstr, const string &message)
    {
        switch(_style)
        {
            case Screen:
                cout << message;
                break;
            case OneFile:
                WriteToClassFile("all", message);
                break;
            case ClassFile:
                WriteToClassFile(levelstr, message);
                break;
            default:
                break;
        }
    }
    // 我们希望提供一个类C的日志接口，并让函数支持可变参数列表
    // Debug, Info, Warning, Error, Fatal
    void LogMessage(int level, const char *format, ...)
    {
        char rightBuffer[1024];
        va_list args;
        va_start(args, format);
        // 此时args指向了可变参数部分
        vsnprintf(rightBuffer, sizeof(rightBuffer), format, args);
        va_end(args);
        char leftBuffer[1024];
        string levelString = LevelToString(level);
        string time = StampExLocalTime();
        
        snprintf(leftBuffer, sizeof(leftBuffer), "[%s][%s] ", levelString.c_str(), time.c_str());

        string logInfo = leftBuffer;
        logInfo += rightBuffer;
        logInfo += "\n";
        WriteLog(levelString, logInfo);

    }

    ~Log()
    {
    }

private:
    int _style;
    string _fileName;
};

Log lg;